package com.gestocasa.view.dialogs.registers;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.gestocasa.model.Area;
import com.gestocasa.model.Attribute;
import com.gestocasa.model.Register;
import com.gestocasa.model.RegisterValue;
import com.gestocasa.view.dialogs.DialogBase;
import com.gestocasa.view.dialogs.helper.DatePickerDialog;
import com.gestocasa.view.dialogs.helper.JAttributeTextField;

public class ValueDialog extends DialogBase {

	private Area area;
	private Register oldRegister;
	private Register newRegister;
	
	private Vector<JLabel> attributesLabels;
	private Vector<JAttributeTextField> attributesEdits;
	
	public ValueDialog(Frame parent, Area area) {
		this(parent, area, null);
	}
	
	public ValueDialog(Frame parent, Area area, Register register) {
		super(parent, "", true);
		setLocationRelativeTo(parent);
		setResizable(false);
		
		this.area = area;
		this.oldRegister = register;
		this.newRegister = (register != null) ? register : new Register(area);
		
		attributesLabels = new Vector<JLabel>();
		attributesEdits = new Vector<JAttributeTextField>();
	}

	private boolean isValidated() {
		boolean valid = true;
		for(JAttributeTextField t : attributesEdits) {
			if(t.isValidated() == false) {
				JOptionPane.showMessageDialog(this, "El valor del campo '"+t.getAttribute().getName()+"' es incorrecto.");
				t.requestFocus();
				
				valid = false;
				break;
			}
		}
		return valid;
	}
	
	private void updateValues() {
		for(JAttributeTextField t : attributesEdits) {
			newRegister.setAttributeValue(t.getAttribute(), t.getValue());
		}
	}
	
	@Override
	protected void initComponents() {
		JAttributeTextField tf = null;
		
		for(Attribute a : area.getAttributes()) {
			tf = new JAttributeTextField(a);
			attributesLabels.add(new JLabel(a.getName()));
			attributesEdits.add(tf);
			tf.setText(a.getDefaultValue());
			if(Attribute.TYPE_DATE.equals(a.getType())) {
				final DialogBase parent = this;
				tf.addMouseListener(new MouseAdapter() {
					public void mousePressed(MouseEvent e) {
						Date date = (Date)((JAttributeTextField)e.getComponent()).getValue();
						DatePickerDialog d = new DatePickerDialog(parent, date);
						d.setVisible(true);
						
						if(d.isCanceled() == false) {
							SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy", Locale.getDefault());
							((JAttributeTextField)e.getComponent()).setText(sdf.format(d.getSelectedDate()));
						}
						
						d.dispose();
						d = null;
					}
				});
			}
			
			if(oldRegister != null) {
				for(RegisterValue rv : oldRegister.getValues()) {
					if(a.equals(rv.getAttribute())) {
						tf.setText(rv.getValue().toString());
					}
				}
			}
		}
	}

	@Override
	protected JPanel makePanels() {
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		panel.add(Box.createVerticalGlue());
		for(int i=0; i<area.getAttributes().size(); i++) {
			panel.add(attributesLabels.get(i));
			panel.add(Box.createRigidArea(new Dimension(0, 5)));
			panel.add(attributesEdits.get(i));
		}
		
		panel.add(Box.createVerticalGlue());
		
		JPanel mainPanel = new JPanel();
		mainPanel.setLayout(new BorderLayout());
		mainPanel.add(panel, BorderLayout.CENTER);
		return mainPanel;
	}

	@Override
	protected Dimension getDefaultSize() {
		int h = 0;
		h += (area.getAttributes().size() * 5);
		h += new JButton("BUTTON").getPreferredSize().height;
		for(JLabel l : attributesLabels) {
			h += l.getPreferredSize().height;
		}
		for(JTextField tf : attributesEdits) {
			h += tf.getPreferredSize().height;
		}
		h+=100;
		return new Dimension(320, h);
	}
	
	
	@Override
	protected void doOK() {
		if(isValidated() == true) {
			updateValues();
			super.doOK();
		}
	}
	
	public Register getRegister() {
		return newRegister;
	}
}
